package com.bwie.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.bwie.common.constants.TokenConstants;
import com.bwie.common.domain.*;
import com.bwie.common.domain.request.CouponRequest;
import com.bwie.common.result.PageResult;
import com.bwie.common.utils.JwtUtils;
import com.bwie.exception.CouponException;
import com.bwie.mapper.CouponMapper;
import com.bwie.service.CouponService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.joda.time.DateTime;
import org.joda.time.LocalDateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @className: CouponServiceImpl
 * @Description: TODO
 * @version: v1.８.0
 * @author: 刘茂锴
 * @date: 2025/10/13 11:12
 */
@Service
public class CouponServiceImpl implements CouponService {
    @Autowired
    private CouponMapper couponMapper;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private RedisTemplate redisTemplate;

    //优惠券列表
    @Override
    public PageResult<Coupon> couponList(CouponRequest couponRequest) {
        PageHelper.startPage(couponRequest.getPageNum(),couponRequest.getPageSize());
        if (couponRequest.getCouponName()!=null && couponRequest.getCouponName().length()>25){
            throw new CouponException("优惠券名称长度不可超过25个字符");
        }
        List<Coupon> list=couponMapper.couponList(couponRequest);
        PageInfo<Coupon> info = new PageInfo<>(list);
        return PageResult.toPageResult(info.getTotal(), info.getList());
    }

    //优惠券类型下拉框
    @Override
    public List<CouponType> couponTypeList() {
        return couponMapper.couponTypeList();
    }

    //活动品牌下拉框
    @Override
    public List<EventBrand> eventBrandList() {
        return couponMapper.eventBrandList();
    }

    //添加优惠券
    @Override
    public void couponAdd(Coupon coupon) {
        //判断优惠券名称长度
        if (coupon.getCouponName().length()>25){
            throw new CouponException("优惠券名称长度不能大于25个字符");
        }
        //给主键id赋值
        DateTime dateTime = new DateTime();
        int year = dateTime.getYear();
        int month = dateTime.getMonthOfYear();
        int dayOfMonth = dateTime.getDayOfMonth();
        String  random= RandomUtil.randomNumbers(3);
        coupon.setId("YHQ"+year+month+dayOfMonth+random);
        //获取登录人信息
        String token = request.getHeader(TokenConstants.TOKEN);
        String userKey = JwtUtils.getUserKey(token);
        User user = (User) redisTemplate.opsForValue().get(TokenConstants.LOGIN_TOKEN_KEY + userKey);
        if (user==null){
            throw new CouponException("请先登录");
        }
        //给字段赋值
        coupon.setUserId(user.getId());
        coupon.setStatus(2);
        coupon.setStoreId(2);
        coupon.setGoodsScopeId(1);
        //添加优惠券
        couponMapper.couponAdd(coupon);
    }


}
